package com.shzu.jwc.ty.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shzu.jwc.ty.bean.TheoreticalExaminationAppointment;
import com.shzu.jwc.ty.common.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.ViewTheoreticalExaminationAppointment;
import com.shzu.jwc.ty.common.ExportExcelUtils;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.service.JwcKnowledgeScoreservice;
import com.shzu.jwc.ty.service.StudentInfoservice;
import com.shzu.jwc.ty.service.SystemConfigservice;
import com.shzu.jwc.ty.service.TheoreticalExaminationservice;

@Controller
@RequestMapping("/jwc/konwledgescore")
public class JwcKnowledgeScorecontroller {

	@Autowired
	StudentInfoservice studentInfoservice;
	
	@Autowired
	JwcKnowledgeScoreservice knowledgeScoreservice;
	
	@Autowired
	SystemConfigservice systemConfigservice;
	
	@Autowired
	TheoreticalExaminationservice theoreticalExaminationservice;
	
	@RequestMapping("/konwledgescore.action")
	public String getkonwledgescore(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		Page page=new Page(knowledgeScoreservice.countknowledgescore(), pn);
		page.setPageSize(20);
		List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.getknowledgescore(page);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("sportsknowledge"));
		model.addAttribute("name", theoreticalExaminationservice.getkowlegetest());
		model.addAttribute("page", page);
		return "jwc/konwledgescore/konwledgescore";
	}

	@RequestMapping("/selectkonwledgescore.action")
	public String selectkonwledgescore(@RequestParam("theoreticalExaminationId") Integer theoreticalExaminationId,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model,HttpSession session) {
		if(theoreticalExaminationId==0) {
			Page page=new Page(knowledgeScoreservice.countknowledgescore(), pn);
			page.setPageSize(20);
			List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.getknowledgescore(page);
			model.addAttribute("data", data);
			model.addAttribute("page", page);
		}else
		{
			Page page=new Page(knowledgeScoreservice.countselectknowledgescore(theoreticalExaminationId), pn);
			page.setPageSize(20);
			List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.selectknowledgescore(theoreticalExaminationId,page);
			model.addAttribute("data", data);
			model.addAttribute("page", page);
		}
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("code", systemConfigservice.getvalue("sportsknowledge"));
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("name", theoreticalExaminationservice.getkowlegetest());
		session.setAttribute("theoreticalExaminationId", theoreticalExaminationId);
		return "jwc/konwledgescore/konwledgescore";
	}

	@RequestMapping("/searchkonwledgescore.action")
	public String searchkonwledgescore(@RequestParam("userStudentInfoNum")String userStudentInfoNum,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
		List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.searchknowledgescore(userStudentInfoNum);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("code", systemConfigservice.getvalue("sportsknowledge"));
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("name", theoreticalExaminationservice.getkowlegetest());
		return "jwc/konwledgescore/konwledgescore";
	}

	@ResponseBody
	@RequestMapping("/updatescore.action")
	public Msg updatescore(@RequestParam("theoreticalExaminationAppointmentScore")double theoreticalExaminationAppointmentScore, @RequestParam("theoreticalExaminationAppointmentId")Integer theoreticalExaminationAppointmentId){
		try {
			knowledgeScoreservice.updatescore(theoreticalExaminationAppointmentScore,theoreticalExaminationAppointmentId);
			return Msg.success("操作成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}
	}

	@RequestMapping("/export.action")
	public ModelAndView export(@RequestParam("userStudentInfoYear") Integer userStudentInfoYear,@RequestParam("userStudentInfoCollege") String userStudentInfoCollege,@RequestParam("theoreticalExaminationId")Integer theoreticalExaminationId,HttpServletResponse response) {
		try {
			List<TheoreticalExaminationAppointment> data=knowledgeScoreservice.upselectknowledgescore(userStudentInfoYear, userStudentInfoCollege,theoreticalExaminationId);
			String code=systemConfigservice.getvalue("sportsknowledge");
			TheoreticalExaminationAppointment appointment=new TheoreticalExaminationAppointment();
			String title="体育知识考试成绩";
			String[] rowsName=new String[] {"取得学年","取得学期","学号","课程／环节代码","课程类别一","课程类别二","课程类别三","环节类别","修读性质","考核方式","辅修标记","补考标记","特殊情况标记","成绩表现形式","原始成绩"};
			List<Object[]> dataList =new ArrayList<Object[]>();
			Object[] objs=null;

			for (int i = 0; i < data.size(); i++) {
				TheoreticalExaminationAppointment po=null;
				po=data.get(i);
				objs = new Object[rowsName.length];
				String[] xq=Semester.getnow().split("-");
				objs[0] = xq[0];
				objs[1] = Integer.parseInt(xq[2])==1?"第一学期":"第二学期";
				objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
				objs[3] = code;
				objs[4] = "必修课";
				objs[5]="";
				objs[6]="";
				objs[7]="";
				objs[8] = "初修";
				objs[9] = "考试";
				if(po.getTheoreticalExaminationAppointmentScore()==null)
					objs[12] = "缺考";
				else
					objs[12]=null;
				objs[14] = po.getTheoreticalExaminationAppointmentScore();
				dataList.add(objs);
			}

			ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);
			ex.exportData();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
